//
//  ManagementClass.m
//  UIAngryBox
//
//  Created by KyuJin on 12. 12. 17..
//  Copyright (c) 2012년 SMJ. All rights reserved.
//

#import "ManagementClass.h"

static id _instance;
@implementation ManagementClass

+ (id) getInstance
{
    if(!_instance)
    {
        _instance = [[ManagementClass alloc] init];
    }
    
    return _instance;
}

- (UIImage*)getUIImageWithFilename:(NSString*)name withType:(NSString*)type
{
    NSString *path = [[NSBundle mainBundle] pathForResource:name ofType:type];
    UIImage *image = [UIImage imageWithContentsOfFile:path];
    
    return image;
}

- (AVAudioPlayer*) getSoundWithFileName:(NSString*)name withType:(NSString*)type withRepeat:(NSInteger)count
{
    NSString *tempPath = [[NSBundle mainBundle] pathForResource:name ofType:type];
    NSURL *tempURL = [[[NSURL alloc] initFileURLWithPath:tempPath] autorelease];
    AVAudioPlayer *tempAudio = [[AVAudioPlayer alloc] initWithContentsOfURL:tempURL error:nil];
 
    [tempAudio setDelegate:self];
    [tempAudio setNumberOfLoops:count];
    
    SAFE_RELEASE(tempURL);
    
    return [tempAudio retain];
}

- (void) showAlertDialog:(NSString*)title with:(NSString*)message
{
    DebugLogDetailed(@"%@", message);
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:title message:message delegate:self cancelButtonTitle:@"확인" otherButtonTitles:nil, nil];
    [alert show];
    [alert release];
}

- (UIAlertView *) waitingDialog
{
    if (!_waitingDialog) {
        _waitingDialog = [[[UIAlertView alloc] initWithTitle:@"" message:nil delegate:self cancelButtonTitle:nil otherButtonTitles: nil] autorelease];
    }
    return _waitingDialog;
}

- (void) showIndicatorWithMessage:(NSString *)message
{
    [self.waitingDialog setTitle:message];
    [self.waitingDialog show];
    UIActivityIndicatorView *indicator = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge];
    
    // Adjust the indicator so it is up a few pixels from the bottom of the alert
    indicator.center = CGPointMake(self.waitingDialog.bounds.size.width / 2, self.waitingDialog.bounds.size.height / 2);
    [indicator startAnimating];
    [self.waitingDialog addSubview:indicator];
    [indicator release];
}

- (void) hideWaitingDialog
{
    if (_waitingDialog) {
        [self.waitingDialog dismissWithClickedButtonIndex:0 animated:YES];
        _waitingDialog = nil;
    }
}


#pragma mark - Audio Delegate
- (void) audioPlayerDidFinishPlaying:(AVAudioPlayer *)player successfully:(BOOL)flag
{
    DebugLogDetailed(@"%s\nSound Release : %@", __FUNCTION__, player);
    @try {
        SAFE_RELEASE(player);
    }
    @catch (NSException *exception) {
        DebugLogDetailed(@"Exception = %@", exception.reason);
    }
    @finally {
        
    }
}

- (void)audioPlayerDecodeErrorDidOccur:(AVAudioPlayer *)player error:(NSError *)error
{
    
}

- (void)audioPlayerBeginInterruption:(AVAudioPlayer *)player
{
    
}

- (void)audioPlayerEndInterruption:(AVAudioPlayer *)player withOptions:(NSUInteger)flags
{
    
}

@end